package com.tedu.charging.service.bill.rabbitmq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.tedu.charging.service.bill.config.RabbitMQInsideMessageConfig;
import com.tedu.charging.common.pojo.vo.WebsocketMessage;
import com.tedu.charging.service.bill.websocket.WebSocketServer;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class AlertConsumer {


    @RabbitListener(queues = RabbitMQInsideMessageConfig.ALERT_QUEUE)
    public void handleBill(WebsocketMessage websocketMessage, Message message, Channel channel) throws Throwable {
        System.out.println("站内信 handleBill" + websocketMessage.toString());
       //把对象转成字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(websocketMessage).toString();
        WebSocketServer.sendInfo(s, websocketMessage.getTo().toString());

        //给消息队列服务器回确认信息
        long tag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(tag, true);
    }
}
